﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL.DateSet;
using BLL.Wrapper;
using System.Collections.Generic;

public partial class ClassManage : System.Web.UI.Page
{
    private static TreeNode node = new TreeNode();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        InitPage();
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        TypeWrapper wrapper = new TypeWrapper();
    }

    protected void ManagementSelectedChanged(object sender, EventArgs e)
    {


    }



    public void InitPage()
    {
        Dictionary<string, int> dict = new Dictionary<string, int>();
        BindSortTreeView(Type, true, "1", "Right.aspx?id=");

    }




    private void CreateChildNode(TreeNode parentNode, DataTable dt, bool isExpanded, string sSelectData, string urls)
    {
        //连接数据，添加排序表达式
        DataRow[] rowList = dt.Select("parentID=" + Convert.ToInt32(parentNode.Value));
        foreach (DataRow row in rowList)
        {
            //创建一个新节点
            TreeNode node = new TreeNode();
            node.Text = row["typeName"].ToString();
            node.Value = row["typeId"].ToString();
            node.Target = "Hello world";
            //node.NavigateUrl = urls;
            //node.Target = "Content";
            node.Expanded = false;

            parentNode.ChildNodes.Add(node);
            //递归调用，创建其他节点
            CreateChildNode(node, dt, isExpanded, sSelectData, urls);
        }
    }

    /// <summary>
    /// 把数据递归添加到树形控件里面
    /// </summary>
    /// <param name="treeView"></param>
    /// <param name="isExpanded"></param>
    /// <param name="sSelectData"></param>
    public void BindSortTreeView(TreeView treeView, bool isExpanded, string sSelectData, string urls)
    {
        TypeWrapper type = new TypeWrapper();

        //将获取的数据放到DataTable里面

        DataTable dt = type.GetData();

        treeView.Nodes.Clear();//清空树的所有节点

        //获取与排序顺序中的筛选器以及指定的状态相匹配的所有System.Data.DataRow 数组  

        DataRow[] dataRowlist = dt.Select("parentID='0'");

        if (dataRowlist.Length <= 0)
        {
            return;
        }

        //创建根节点
        TreeNode rootNode = new TreeNode();
        //设置根节点的属性
        rootNode.Text = dataRowlist[0]["typeName"].ToString();
        //设置根节点的值
        rootNode.Value = dataRowlist[0]["typeID"].ToString();
        rootNode.Target = "Hello world";
        rootNode.Expanded = true;
        //添加根节点
        treeView.Nodes.Add(rootNode);
        CreateChildNode(rootNode, dt, isExpanded, sSelectData, urls);
    }

    protected void NodeChange(object sender, System.EventArgs e)
    {
        //Management.SelectedNode.Expand();
        // Management.SelectedNode.Selected = true;
        //Management.SelectedNode.Checked = true;
    }

    protected void NodeChecked(object sender, System.EventArgs e)
    {

    }

    protected void display(object sender, EventArgs e)
    {
        string text = Type.SelectedNode.Target;
        //Right.Type1.Text = Management.SelectedNode.Text;
        // Parent.Right.Type1.Text = Management.SelectedNode.Text;
    }

    protected void AddSubNode(object sender, EventArgs e)
    {
        string text = TextBox1.Text;
        TypeWrapper wrapper = new TypeWrapper();
        TreeNode node = new TreeNode();
        node = Type.SelectedNode;
        if (Type.SelectedNode == null)
        {
            text = "Hello ";
            return;
        }
        int nodeid = Convert.ToInt32(Type.SelectedNode.Value);
        int level = wrapper.getLevelByName(node.Text);
        DAL.DateSet.Type.TypeDataTable table = new DAL.DateSet.Type.TypeDataTable();
        DAL.DateSet.Type.TypeRow row = table.NewTypeRow();
        row.parentID = nodeid;
        row.typeCreateDate = DateTime.Now;
        row.typeCreator = "someone";
        row.typeFlag = 1;
        row.typeLevel = level + 1;
        row.typeName = TextBox1.Text;
        row.englishName = TextBox2.Text;

        wrapper.addARecord(row);
        InitPage();
    }

    protected void SelectedChanged(object sender, EventArgs e)
    {
        if (node.ValuePath.Length > 0)
        {
            Type.FindNode(node.ValuePath).ImageUrl = "";
        }
        node = Type.SelectedNode;
        Type.SelectedNode.Checked = true;
        Type.SelectedNode.ImageUrl = "~Image/11.jpg";
    }
}
